<template>
  <div class="app-container">
    <div class="form-box">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-tabs type="border-card" class="form-tabs">
          <el-tab-pane :label="$t('common.edit_info')" class="form-tabs-tab-pane">
            <!-- 标题 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.title')" prop="title">
                  <el-input
                    v-model="form.title"
                    :placeholder="$t('info.title_tips')"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 副标题 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.subtitle')"
                  prop="subtitle"
                >
                  <el-input
                    v-model="form.subtitle"
                    :placeholder="$t('info.subtitle_tips')"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 来源 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.source')" prop="source">
                  <el-input
                    v-model="form.source"
                    :placeholder="$t('infoNews.source_tips')"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 作者 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.author')" prop="author">
                  <el-input
                    v-model="form.author"
                    :placeholder="$t('infoNews.author_tips')"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 封面图 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.cover_image')"
                  prop="cover_image"
                >
                  <jsp-image-upload
                    :title="$t('common.cover_image')"
                    :default-list="cover_imageList"
                    :action="fileUploadConfig.imageAction"
                    :headers="fileUploadConfig.headers"
                    :size="Number(fileUploadConfig.allowImageSize)"
                    :accept="fileUploadConfig.allowImageTypeList"
                    @error="(error)=>{error!=null && $message.error(error)}"
                    @change="handleCoverImageUploadChange"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 关键词 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.keywords')" prop="keywords">
                  <jsp-tags
                    :data="form.keywords"
                    :limit="20"
                    @change="(tags) => (form.keywords = tags)"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 摘要 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.description')"
                  prop="description"
                >
                  <el-input
                    v-model="form.description"
                    type="textarea"
                    :placeholder="$t('common.description_tips')"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 内容 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.content')" prop="content">
                  <jsp-ckeditor
                    id="content"
                    v-model="form.content"
                    :config="{
                      'filebrowserUploadUrl': fileUploadConfig.action,
                      'filebrowserImageUploadUrl': fileUploadConfig.imageAction,
                      'headers' : fileUploadConfig.headers,
                      'filebrowserUploadInputName': 'file',
                    }"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 链接地址 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.href')" prop="href">
                  <el-input
                    v-model="form.href"
                    :placeholder="$t('common.href_tips')"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 打开方式 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.href_target')"
                  prop="href_target"
                >
                  <el-select
                    v-model="form.href_target"
                    :placeholder="$t('common.href_target_tips')"
                    clearable
                  >
                    <el-option
                      v-for="item in dictionaryService.getItems('hrefTargetTypeVar')"
                      :key="item.id"
                      :label="item.name"
                      :value="item.value"
                    />

                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 链接关系XFN -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.rel')" prop="rel">
                  <el-select
                    v-model="form.rel"
                    :placeholder="$t('common.rel_tips')"
                    clearable
                  >
                    <el-option
                      v-for="item in dictionaryService.getItems('relTypeVar')"
                      :key="item.id"
                      :label="item.name"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 推荐类型 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.recommend_type')"
                  prop="recommend_type"
                >
                  <el-checkbox-group v-model="form.recommend_type">
                    <el-checkbox v-for="item in dictionaryService.getItems('recommendTypeVar')" :key="item.id" :label="item.value">{{ item.name }}</el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 是否置顶 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.top')" prop="top">
                  <el-select
                    v-model="form.top"
                    :placeholder="$t('common.pleaseSelect')"
                    clearable
                  >
                    <el-option
                      v-for="item in dictionaryService.getItems('topTypeVar')"
                      :key="item.id"
                      :label="item.name"
                      :value="Number(item.value)"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 创建时间 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.created_at')"
                  prop="created_at"
                >
                  <el-date-picker
                    v-model="form.created_at"
                    type="datetime"
                    :placeholder="$t('common.created_at_tips')"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :editable="false"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 发布状态 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.status')"
                  prop="status"
                >
                  <el-select
                    v-model.number="form.status"
                    :placeholder="$t('common.pleaseSelect')+$t('common.status')"
                    clearable
                  >
                    <el-option
                      v-for="item in dictionaryService.getItems('statusVar')"
                      :key="item.id"
                      :label="item.name"
                      :value="Number(item.value)"
                    />

                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>

        <el-row :gutter="20" type="flex" justify="center" class="btn-row">
          <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
            <el-form-item>
              <el-button
                type="primary"
                :loading="submitLoading"
                :disabled="routerParams.id>0&&!checkPermission(['admin.infoNews.update']) || !routerParams.id&&!checkPermission(['admin.infoNews.store'])"
                @click="onSubmit"
              >{{
                $t('common.submit')
              }}</el-button>
              <el-button @click="onCancel">{{ $t('common.cancel') }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import JspCkeditor from '@/components/JspCkeditor'
import JspImageUpload from '@/components/JspImageUpload'
import JspTags from '@/components/JspTags'
import { show, update, store } from '@/api/info-news'
import { mapGetters } from 'vuex'
import { loadingStart } from '@/utils/loading-service'
import checkPermission from '@/utils/permission'

// export
export default {
  name: 'InfoNewsEdit',
  components: {
    JspCkeditor,
    JspImageUpload,
    JspTags
  },
  data() {
    return {
      submitLoading: false,
      form: {
        title: '',
        subtitle: '',
        cover_image: 0,
        keywords: [],
        description: '',
        href: '',
        href_target: '_blank',
        rel: '',
        recommend_type: [],
        top: 0,
        created_at: '',
        status: 1,
        // 扩展
        source: '',
        author: '',
        content: ''
      },
      // 封面图列表
      cover_imageList: [],
      // 表单输入校验 ，通过prop="name"属性进行控制
      rules: {
        title: [
          { required: true, message: this.$t('info.title_tips'), trigger: 'blur' },
          { min: 2, max: 100, message: this.$t('info.title_length_tips'), trigger: 'blur' }
        ]
      },
      // 路由参数
      routerParams: {},
      infoCategory: {}
    }
  },
  computed: {
    ...mapGetters(['fileUploadConfig'])
  },
  created() {
    this.routerParams = this.$route.params
    this.infoCategory = this.$route.meta.infoCategory
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    checkPermission,
    async getDetail() {
      const { id } = this.routerParams
      if (id > 0) {
        const loading = loadingStart()
        await show(this.infoCategory.id, id).then((response) => {
          this.form = response.data
        }).catch(() => {
          this.closeBack()
        })
        // 封面图
        this.cover_imageList = this.form.cover_image
        this.form.cover_image = this.form.cover_image.map((item) => {
          return item.id
        }).join(',')
        loading.close()
      }
    },
    onSubmit() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.submitLoading = true
          const back_url_name = 'InfoCategory' + this.infoCategory.id + 'InfoNews'
          let response
          if (this.routerParams.id) {
            response = await update(this.infoCategory.id, this.routerParams.id, this.form).catch(() => {})
          } else {
            response = await store(this.infoCategory.id, this.form).catch()
          }
          if (response instanceof Object) {
            this.$message.success(response.message)
            this.closeBack(back_url_name)
          }
          this.submitLoading = false
        } else {
          this.$message.error(this.$t('common.pleaseFillRequired'))
          return false
        }
      })
    },
    onCancel() {
      this.closeBack()
    },
    handleCoverImageUploadChange(result) {
      this.form.cover_image = result.ids
    }
  }
}
</script>

<style scoped lang="scss">
</style>
